package com.umgsai.thread.thread20;

public class OutClass {
	static class InnerClass1{
		public void method1(InnerClass2 class2) {
			String threadName = Thread.currentThread().getName();
			synchronized (class2) {
				System.out.println(threadName + "进入InnerClass1的method1方法");
				for (int i = 0; i < 5; i++) {
					System.out.println("i=" + i);
					try {
						Thread.sleep(100);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				System.out.println(threadName + "离开InnerClass1的method1方法");
			}
		}
		
		public synchronized void method2() {
			String threadName = Thread.currentThread().getName();
			System.out.println(threadName + "进入InnerClass1的method2方法");
			for (int j = 0; j < 5; j++) {
				System.out.println("j=" + j);
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println(threadName + "进入InnerClass1的method2方法");
		}
	}
	
	static class InnerClass2{
		public synchronized void method1() {
			String threadName = Thread.currentThread().getName();
			System.out.println(threadName + "进入InnerClass2的method1方法");
			for (int k = 0; k < 5; k++) {
				System.out.println("k=" + k);
				try {
					Thread.sleep(100);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println(threadName + "离开InnerClass2的method1方法");
		}
	}
	
	public static void main(String[] args) {
		final InnerClass1 class1 = new InnerClass1();
		final InnerClass2 class2 = new InnerClass2();
		Thread t1 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				class1.method1(class2);
			}
		}, "T1");
		
		Thread t2 = new Thread(new Runnable() {
			public void run() {
				class1.method2();
			}
		}, "T2");
		
		Thread t3 = new Thread(new Runnable() {
			
			@Override
			public void run() {
				class2.method1();
			}
		}, "T3");
		
		t1.start();
		t2.start();
		t3.start();
	}
}
